package xyz.zhouzhaodong.crm.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import xyz.zhouzhaodong.crm.dto.CostDto;
import xyz.zhouzhaodong.crm.dto.CostResultDto;
import xyz.zhouzhaodong.crm.entity.Cost;
import xyz.zhouzhaodong.crm.entity.Customer;
import xyz.zhouzhaodong.crm.mapper.CostMapper;
import xyz.zhouzhaodong.crm.mapper.CustomerMapper;
import xyz.zhouzhaodong.crm.service.ICostService;
import xyz.zhouzhaodong.crm.util.RestMessage;

import javax.annotation.Resource;
import java.util.*;

/**
 * <p>
 * 费用表 服务实现类
 * </p>
 *
 * @author zhouzhaodong
 * @since 2020-06-18
 */
@Service
public class CostServiceImpl extends ServiceImpl<CostMapper, Cost> implements ICostService {

    @Resource
    CostMapper costMapper;

    @Resource
    CustomerMapper customerMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public RestMessage addCost(CostDto costDto) {
        try {
            Cost cost = costDtoToCost(costDto);
            String id = UUID.randomUUID().toString().replaceAll("-", "");
            cost.setId(id);
            cost.setCreateUser(costDto.getStaffId());
            cost.setCreateTime(new Date());
            costMapper.insert(cost);
            return RestMessage.success(id);
        } catch (Exception e) {
            return RestMessage.failed(e.getMessage());
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public RestMessage deleteCost(CostDto costDto) {
        try {
            Cost cost = new Cost();
            cost.setId(costDto.getId());
            cost.setFlag(1);
            cost.setUpdateUser(costDto.getStaffId());
            cost.setUpdateTime(new Date());
            costMapper.updateById(cost);
            return RestMessage.success();
        } catch (Exception e) {
            return RestMessage.failed(e.getMessage());
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public RestMessage updateCost(CostDto costDto) {
        try {
            Cost cost = costDtoToCost(costDto);
            cost.setUpdateUser(costDto.getStaffId());
            cost.setUpdateTime(new Date());
            costMapper.updateById(cost);
            return RestMessage.success();
        } catch (Exception e) {
            return RestMessage.failed(e.getMessage());
        }
    }

    @Override
    public RestMessage selectCost(CostDto costDto) {
        QueryWrapper<Cost> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("customer_id", costDto.getCustomerId());
        if(costDto.getFlag() != null){
            queryWrapper.eq("flag", costDto.getFlag());
        }
        List<Cost> list = costMapper.selectList(queryWrapper);
        return RestMessage.success(list);
    }

    @Override
    public RestMessage selectCostList(CostDto costDto) {
        Integer flag = costDto.getFlag();
        if(flag == null){
            costDto.setFlag(0);
        }
        List<CostDto> list = costMapper.selectContract(costDto);
        // 获取customerId
        List<Cost> customerIdList = costMapper.selectCustomerIdList(costDto);
        //这行是重点，表示从pageNum页开始，每页pageSize条数据
        PageHelper.startPage(costDto.getPageNo(), costDto.getPageSize() == 0 ? 10 : costDto.getPageSize());
        List<CostResultDto> listResult = new ArrayList<>();
        for(Cost cost : customerIdList){
            CostResultDto costResultDto = new CostResultDto();
            for(CostDto c : list){
                if(Objects.equals(c.getCustomerId(), cost.getCustomerId())){
                    // 相等就需要组合
                    if(Objects.equals(c.getFeeName(), "申请费")){
                        String money = c.getMoney();
                        String currency = c.getCurrency();
                        if (currency != null && !"".equals(currency)){
                            money = money + " (" + currency + ")";
                        }
                        costResultDto.setApplicationFee(money);
                        continue;
                    }
                    if(Objects.equals(c.getFeeName(), "尾款")){
                        String money = c.getMoney();
                        String currency = c.getCurrency();
                        if (currency != null && !"".equals(currency)){
                            money = money + " (" + currency + ")";
                        }
                        costResultDto.setFinalPayment(money);
                        continue;
                    }
                    if(Objects.equals(c.getFeeName(), "学校押金")){
                        String money = c.getMoney();
                        String currency = c.getCurrency();
                        if (currency != null && !"".equals(currency)){
                            money = money + " (" + currency + ")";
                        }
                        costResultDto.setSchoolDeposit(money);
                        continue;
                    }
                    if(Objects.equals(c.getFeeName(), "服务费")){
                        String money = c.getMoney();
                        String currency = c.getCurrency();
                        if (currency != null && !"".equals(currency)){
                            money = money + " (" + currency + ")";
                        }
                        costResultDto.setServiceCharge(money);
                        continue;
                    }
                    if(Objects.equals(c.getFeeName(), "机票")){
                        String money = c.getMoney();
                        String currency = c.getCurrency();
                        if (currency != null && !"".equals(currency)){
                            money = money + " (" + currency + ")";
                        }
                        costResultDto.setPlaneTicket(money);
                        continue;
                    }
                    if(Objects.equals(c.getFeeName(), "真实机票")){
                        String money = c.getMoney();
                        String currency = c.getCurrency();
                        if (currency != null && !"".equals(currency)){
                            money = money + " (" + currency + ")";
                        }
                        costResultDto.setRealPlaneTicket(money);
                        continue;
                    }
                    if(Objects.equals(c.getFeeName(), "接机VIP")){
                        String money = c.getMoney();
                        String currency = c.getCurrency();
                        if (currency != null && !"".equals(currency)){
                            money = money + " (" + currency + ")";
                        }
                        costResultDto.setPickUpVip(money);
                        continue;
                    }
                    if(Objects.equals(c.getFeeName(), "All-in")){
                        String money = c.getMoney();
                        String currency = c.getCurrency();
                        if (currency != null && !"".equals(currency)){
                            money = money + " (" + currency + ")";
                        }
                        costResultDto.setAllIn(money);
                        continue;
                    }
                    if(Objects.equals(c.getFeeName(), "代收学费")){
                        String money = c.getMoney();
                        String currency = c.getCurrency();
                        if (currency != null && !"".equals(currency)){
                            money = money + " (" + currency + ")";
                        }
                        costResultDto.setCollectionTuition(money);
                        continue;
                    }
                    if(Objects.equals(c.getFeeName(), "学费")){
                        String money = c.getMoney();
                        String currency = c.getCurrency();
                        if (currency != null && !"".equals(currency)){
                            money = money + " (" + currency + ")";
                        }
                        costResultDto.setTuition(money);
                        continue;
                    }
                    if(Objects.equals(c.getFeeName(), "退费")){
                        String money = c.getMoney();
                        String currency = c.getCurrency();
                        if (currency != null && !"".equals(currency)){
                            money = money + " (" + currency + ")";
                        }
                        costResultDto.setRefund(money);
                        continue;
                    }
                    if(Objects.equals(c.getFeeName(), "备注")){
                        costResultDto.setRemark(c.getRemark());
                    }
                }
            }

            Customer customer = customerMapper.selectById(cost.getCustomerId());
            costResultDto.setCustomer(customer);
            listResult.add(costResultDto);
        }
        PageInfo<CostResultDto> pageInfo = new PageInfo<>(listResult);
        return RestMessage.success(pageInfo);
    }

    private Cost costDtoToCost(CostDto costDto) {
        Cost cost = new Cost();
        cost.setId(costDto.getId());
        cost.setCustomerId(costDto.getCustomerId());
        cost.setFeeName(costDto.getFeeName());
        cost.setMoney(costDto.getMoney());
        cost.setCurrency(costDto.getCurrency());
        cost.setPaymentMethod(costDto.getPaymentMethod());
        cost.setCollectionDate(costDto.getCollectionDate());
        cost.setRemark(costDto.getRemark());
        cost.setFlag(costDto.getFlag());
        return cost;
    }

}
